//% color="#348aed" iconWidth=50 iconHeight=40
namespace UNIHIKER_K10_BOX{

    //% block="传感器" blockType="tag"
    export function tagtest1() {}
    
    //% block="Read angle sensor" blockType="reporter"  
    export function ANGLEvalue(parameter: any, block: any) { 

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.getKnobValue()`);
   
    }

    //% block="Read sound intensity" blockType="reporter"  
    export function SoundVolume(parameter: any, block: any) { 
        
        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.getSoundVolume()`);

    }


    //% block="---"
    export function noteSep6() {}

    //% block="The [DIRECTION] button is pressed ?" blockType="boolean"  
    //% DIRECTION.shadow="dropdown" DIRECTION.options="DIRECTION"
    export function READbutton(parameter: any, block: any) { 
        let direction = parameter.DIRECTION.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`digital_read(${direction}) == 0`)

    }

    //% block="The conductance is turned on ?" blockType="boolean"  
    export function Conductance(parameter: any, block: any) { 

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);
``
        Generator.addCode(`digital_read(eP10)`)
   
    }

    //% block="Infrared sensor detects object ?" blockType="boolean"  
    export function READinfrared(parameter: any, block: any) { 

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`digital_read(eP13) == 0`)
    
    }

    //% block="---"
    export function noteSep7() {}

    //% block="Initialize RFID module [VER]" blockType="command"  
    //% VER.shadow="dropdown" VER.options="VER"
    export function RFID_init(parameter: any, block: any) { 
        let ver = parameter.VER.code;

        if (ver == "V1"){
            Generator.addInclude('RFID1', '#include <DFRobot_PN7151.h>');
            Generator.addObject(`RFID2`, `DFRobot_PN7150_I2C`, `RFID;`);
            Generator.addSetup(`RFID.begin`, `
while (!(RFID.begin(NXPNCI_MODE_RW))) {
  Serial.println("init fail!");
}
Serial.println("successful connection.");
            `);
        }else{
            Generator.addInclude('RFID1', '#include <DFRobot_FM17550.h>');
            Generator.addObject(`RFID2`, `DFRobot_FM17550`, `RFID;`);
            Generator.addSetup(`RFID.begin`, `
while (!(RFID.begin())) {
  Serial.println("init fail!");
}
Serial.println("successful connection.");
            `);
        }

        
    }

    //% block="Write [DATA] to byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="command" 
    //% DATA.shadow="range" DATA.params.min=0 DATA.params.max=255 DATA.defl=200
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_write(parameter: any, block: any) { 
        let data=parameter.DATA.code;
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.writeData(${block_num}, ${byte_num}, ${data});`);

    }

    //% block="Read all data from block [BLOCK_NUM] of the RFID module(string)" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_read_all(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;

        Generator.addCode(`RFID.readData(${block_num})`);

    }

    //% block="Read byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    export function RFID_read(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.readData(${block_num}, ${byte_num})`);

    }

    //% block="Read the UID of the tag card" blockType="reporter" 
    export function RFID_read_UID(parameter: any, block: any) { 

        Generator.addCode(`RFID.readUid()`);

    }

    //% block="Detected card. UID:[UID]?" blockType="boolean" 
    //% UID.shadow="string" UID.defl="acee50e3"
    export function RFID_SCAN_UID(parameter: any, block: any) {   
        let uid=parameter.UID.code;

        Generator.addCode(`RFID.scan(${uid})`);

    }

    //% block="Detected card?" blockType="boolean" 
    export function RFID_SCAN(parameter: any, block: any) { 

        Generator.addCode(`RFID.scan()`);

    }

    //% block="---"
    export function noteSep1() {}

    //% block="执行器" blockType="tag"
    export function tagtest2() {}

    //% block="Set [COLOR] LED [LEVEL]" blockType="command"  
    //% COLOR.shadow="dropdown" COLOR.options="COLOR"
    //% LEVEL.shadow="dropdown" LEVEL.options="LEVEL"
    export function LEDdigital(parameter: any, block: any) { 
        let color=parameter.COLOR.code;
        let level=parameter.LEVEL.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.${color}_digital(${level});`);
   
    }

    //% block="Set [COLOR] LED brightness[BRIGHT]" blockType="command"  
    //% COLOR.shadow="dropdown" COLOR.options="COLOR"
    //% BRIGHT.shadow="range" BRIGHT.params.min=0 BRIGHT.params.max=255 BRIGHT.defl=200
    export function LEDanalog(parameter: any, block: any) { 
        let color=parameter.COLOR.code;
        let bright=parameter.BRIGHT.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.${color}_analog(${bright});`);
   
    }

    //% block="---"
    export function noteSep8() {

    } 

    //% block="Set the frequency of the buzzer to [NUM]" blockType="command"  

    //% NUM.shadow="note" NUM.defl=131
    export function SetBuzzer(parameter: any, block: any) { 
        let num=parameter.NUM.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.setBuzzer(${num});`);
   
    }

    //% block="Set the buzzer to stop." blockType="command"  
    export function StopBuzzer(parameter: any, block: any) { 

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.stopBuzzer();`);
   
    }

    //% block="---"
    export function noteSep9() {

    } 

    //% block="Set water pump [LEVEL]" blockType="command"  
    //% LEVEL.shadow="dropdown" LEVEL.options="LEVEL1"
    export function waterpump_digital(parameter: any, block: any) { 
        let level=parameter.LEVEL.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`digital_write(eP4, ${level});`)
   
    }

    //% block="---"
    export function noteSep2() {

    }

    //% block="Initializes the RGB light" blockType="command"  
    export function INITRGB(parameter: any, block: any) { 

        Generator.addInclude('DFRobot_NeoPixel', '#include <DFRobot_NeoPixelNew.h>');
        Generator.addObject(`DFRobot_NeoPixel`, `DFRobot_NeoPixelNew`, `neoPixel_box;`);
        Generator.addSetup(`neoPixel_box.begin`, `neoPixel_box.begin(46, 4);`);
   
    }

    //% block="RGB lights Set [BRIGHT]" blockType="command"  
    //% BRIGHT.shadow="range" BRIGHT.params.min=0 BRIGHT.params.max=255 BRIGHT.defl=255
    export function RGBbrightness(parameter: any, block: any) { 
        let bright=parameter.BRIGHT.code;

        Generator.addCode(`neoPixel_box.setBrightness(${bright});`);
   
    }

    //% block="All RGB lights are off" blockType="command"  
    export function RGBoff(parameter: any, block: any) { 

        Generator.addCode(`neoPixel_box.clear();`)
   
    }

    //% block="RGB lights [NUMBER1] to [NUMBER2] display [COLOR]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% COLOR.shadow="colorPalette"
    export function RGBcolor2(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let color=parameter.COLOR.code; 

        Generator.addCode(`neoPixel_box.setRangeColor(${number1},${number2},${color});`)
   
    }

    //% block="RGB lights [NUMBER1] to [NUMBER2] display gradient color. tone from [RANGE1] to [RANGE2]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% RANGE1.shadow="range" RANGE1.params.min=0 RANGE1.params.max=360 RANGE1.defl=1
    //% RANGE2.shadow="range" RANGE2.params.min=0 RANGE2.params.max=360 RANGE2.defl=360
    export function RGBtone(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let range1=parameter.RANGE1.code; 
        let range2=parameter.RANGE2.code; 

        Generator.addCode(`neoPixel_box.showRainbow(${number1},${number2},${range1},${range2});`)
   
    }

    //% block="RGB light moves [NUM] unit" blockType="command"  
    //% NUM.shadow="number" NUM1.defl="0"
    export function RGBmove1(parameter: any, block: any) { 
        let num=parameter.NUM.code;

        Generator.addCode(`neoPixel_box.shift(${num});`)
   
    }

    //% block="The RGB light rotates [NUM] unit" blockType="command"  
    //% NUM.shadow="number" NUM.defl="0"
    export function RGBmove2(parameter: any, block: any) { 
        let num=parameter.NUM.code;

        Generator.addCode(`neoPixel_box.rotate(${num});`)
   
    }

    //% block="RED[RED] GREEN[GREEN] BLUE[BLUE]" blockType="reporter"  
    //% RED.shadow="range" RED.params.min=0 RED.params.max=255 RED.defl=255
    //% GREEN.shadow="range" GREEN.params.min=0 GREEN.params.max=255 GREEN.defl=255
    //% BLUE.shadow="range" BLUE.params.min=0 BLUE.params.max=255 BLUE.defl=255
    export function color(parameter: any, block: any) { 
        let red=parameter.RED.code;
        let green=parameter.GREEN.code;
        let blue=parameter.BLUE.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.rgbToColor(round(${red}), round(${green}), round(${blue}))`);

   
    }

    //% block="---"
    export function noteSep3() {}

    //% block="高配功能" blockType="tag"
    export function tagtest3() {}

    //% block="read infrared receiver value" blockType="reporter"  
    export function IRValue(parameter: any, block: any) { 
        
        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);
        
        Generator.addCode(`box.getIRValue()`);

    }

    //% block="Read the acceleration [AXIS] value." blockType="reporter" 
    //% AXIS.shadow="dropdown" AXIS.options="AXIS"

    export function acceleration(parameter: any, block: any) { 
        let axis = parameter.AXIS.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);
        Generator.addSetup(`initQMI8658C`, `box.initQMI8658C();`);

        Generator.addCode(`box.get${axis}()`);

    }

    //% block="Read the gyroscope [AXIS] value." blockType="reporter"  
    //% AXIS.shadow="dropdown" AXIS.options="AXIS1"

    export function gyroscope(parameter: any, block: any) { 
        let axis = parameter.AXIS.code;


        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);
        Generator.addSetup(`initQMI8658C`, `box.initQMI8658C();`);
        
        Generator.addCode(`box.getGyroscope${axis}dps()`);

    }

    //% block="Read the ultrasonic sensor distance" blockType="reporter"  
    export function SR04ultrasonic(parameter: any, block: any) { 

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        Generator.addCode(`box.getSR04Value()`);
   
    }

    //% block="---"
    export function noteSep4() {}
    
    //% block="Initialize  line tracker at I2C address 0x7C" blockType="command"  
    export function Tracker_init(parameter: any, block: any) { 

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);
   
    }

    //% block="Calibrate line tracker sensor [PROBE] threshold to [THRESHOLD_SET]" blockType="command" 
    //% PROBE.shadow="dropdown" PROBE.options="PROBE"
    //% THRESHOLD_SET.shadow="range" THRESHOLD_SET.params.min=0 THRESHOLD_SET.params.max=255 THRESHOLD_SET.defl=150
    export function THRESHOLD_SET(parameter: any, block: any) { 
        let probe=parameter.PROBE.code;
        let threshold_set=parameter.THRESHOLD_SET.code;

        Generator.addCode(`box.setLinePatrolThreshold(${probe}, ${threshold_set});`);

    }

    //% block="Get all line sensor thresholds(array)" blockType="reporter" 
    export function THRESHOLD_GET(parameter: any, block: any) { 

        Generator.addCode(`box.getLinePatrolThresholdAll()`);

    }

    //% block="Read all probe analog values(array)" blockType="reporter" 
    export function Tracker_read_all_analog(parameter: any, block: any) { 

        Generator.addCode(`box.getLinePatrolValueAll()`);

    }

    //% block="Read all probe digital values(string)" blockType="reporter" 
    export function Tracker_read_all_digital(parameter: any, block: any) { 

        Generator.addCode(`box.getLinePatrolStatusAll()`);

    }

    //% block="Read all digital probe values equal [VALUE]?" blockType="boolean" 
    //% VALUE.shadow="string" VALUE.defl="00000"
    export function Tracker_read_all_boolean(parameter: any, block: any) { 
        
        let value=parameter.VALUE.code;
        Generator.addCode(`box.getLinePatrolStatusAll() == ${value}`);
    }

    //% block="Read digital value of line sensor [PROBE]" blockType="boolean"
    //% PROBE.shadow="dropdown" PROBE.options="PROBE"
    export function Tracker_read_digital(parameter: any, block: any) { 
        
        let probe=parameter.PROBE.code;
        Generator.addCode(`box.getLinePatrolValue(${probe})`);
    }

    //% block="---"
    export function noteSep5() {}

    //% block="Motor [MOTOR] [TURN] at [SPEED]" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    //% TURN.shadow="dropdown" TURN.options="TURN" 
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=255 SPEED.defl=200
    export function MOTORrotation(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;
        let turn=parameter.TURN.code;
        let speed=parameter.SPEED.code;


        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        if (motor == "M1"){
            Generator.addCode(`box.setMotor1Run(${turn},${speed});`);
        }
        else if (motor == "M2"){
            Generator.addCode(`box.setMotor2Run(${turn},${speed});`);
        }
        else{
            Generator.addCode(`box.setMotor1Run(${turn},${speed});`);
            Generator.addCode(`box.setMotor2Run(${turn},${speed});`);
        }
        
    }

    //% block="Motor [MOTOR] stop" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    export function MOTORstop(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;

        Generator.addInclude('DFRobot_K10Box', '#include <DFRobot_K10Box.h>');
        Generator.addObject(`DFRobot_K10Box`, `DFRobot_K10Box`, `box;`);
        Generator.addSetup(`DFRobot_K10Box.begin`, `box.begin();`);

        if (motor == "M1"){
            Generator.addCode(`box.setMotor1Run(1,0);`);
        }
        else if (motor == "M2"){
            Generator.addCode(`box.setMotor2Run(1,0);`);
        }
        else{
            Generator.addCode(`box.setMotor1Run(1,0);`);
            Generator.addCode(`box.setMotor2Run(1,0);`);
        }
   
    }


}

